//
//  BusinessViewController.m
//  AipuTel
//
//  Created by cretve on 15/4/11.
//  Copyright (c) 2015年 aipu. All rights reserved.
//

#import "BusinessViewController.h"
#import <UIImageView+AFNetworking.h>

@interface BusinessViewController ()

@property (strong, nonatomic) NSArray* businesses;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation BusinessViewController

- (void)awakeFromNib
{
    self.tabBarItem.title = @"商户列表";
    self.tabBarItem.image = [[UIImage imageNamed:@"business"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.tabBarItem.selectedImage = [[UIImage imageNamed:@"business"] imageWithWhiteMask];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"点话通";
    
    [self getBusinesses];
}
     
- (void)getBusinesses
{
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"Businesses" : @[]}];
    self.businesses = [[NSUserDefaults standardUserDefaults] arrayForKey:@"Businesses"];
    if(_businesses.count > 0)
    {
        [_tableView reloadData];
    }
    
    __weak BusinessViewController* weakSelf = self;
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/html", @"text/javascript", nil];
    ((AFJSONResponseSerializer*)manager.responseSerializer).removesKeysWithNullValues = YES;
    [manager GET:@"http://202.98.133.196/ddserver/getbusinesslist" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if([responseObject[@"errno"] integerValue] == 0)
        {
            weakSelf.businesses = responseObject[@"data"];
            [weakSelf.tableView reloadData];
            
            [[NSUserDefaults standardUserDefaults] setObject:weakSelf.businesses forKey:@"Businesses"];
            [[NSUserDefaults standardUserDefaults] synchronize];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

#pragma mark -

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _businesses.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    UIImageView* imageView = (UIImageView* )[cell.contentView viewWithTag:1];
    [imageView setImageWithURL:[NSURL URLWithString:_businesses[indexPath.row][@"smallImage"]]];
    
    UILabel* titleLabel = (UILabel* )[cell.contentView viewWithTag:2];
    titleLabel.text = _businesses[indexPath.row][@"businessname"];
    
    UILabel* addressLabel = (UILabel* )[cell.contentView viewWithTag:3];
    addressLabel.text = _businesses[indexPath.row][@"address"];
    
    UILabel* telLabel = (UILabel* )[cell.contentView viewWithTag:4];
    telLabel.text = _businesses[indexPath.row][@"tel"];
    
    UIButton* callButton = (UIButton* )[cell.contentView viewWithTag:5];
    if(callButton.allTargets.count == 0)
    {
        __weak BusinessViewController* weakSelf = self;
        [callButton bk_addEventHandler:^(UIButton* sender) {
            NSInteger index = [tableView indexPathForCell:(UITableViewCell*)sender.superview.superview].row;
            NSString* tel = weakSelf.businesses[index][@"tel"];
            [UIAlertView bk_showAlertViewWithTitle:[NSString stringWithFormat:@"拨打 %@ ？", tel] message:nil cancelButtonTitle:@"取消" otherButtonTitles:@[@"确认"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                if(buttonIndex == 1)
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", tel]]];
            }];
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return cell;
}

@end
